<?php
include('/home/wwwroot/football.laulei.com/protected/extensions/simple_html_dom.php');
//获取并更新北京单场当前期的亚盘数据
//截止时间是比赛前15分钟
class GetOddsInfosAsiaAllCommand extends CConsoleCommand  
{
    public function run($args) {
    	$gameLastIssue=GameInfo::model()->findAllBySql("select distinct issue from tbl_gameinfo order by issue desc", array(), array());

    	foreach ($gameLastIssue as $keyIssue => $valueIssue) {
    		$lstGameInfo=GameInfo::model()->findAll(
				array(
					'select'=>'*',
					'condition'=>'issue=:issue',
					'params'=>array(':issue'=>$valueIssue->issue),
					)
				);
	    		foreach ($lstGameInfo as $key => $value) {
				$oddsUrl=sprintf("http://odds.500.com/fenxi/yazhi-%s-show-2", $value->gameid);
				printf("%s\n", $oddsUrl);
				$htmlOdds = file_get_html($oddsUrl);
		    	if(!isset($htmlOdds)){
		    		continue;
		    	}
		    	//平均值
		    	$tmp=$htmlOdds->find('td[id=avges1]', 0);
		    	if(isset($tmp)){
			    	$companyid='-1';
			    	$companyname='平均值';
			    	$oddshome=trim($htmlOdds->find('td[id=avges1]', 0)->plaintext);
		    		$rate=trim($htmlOdds->find('td[id=avgeh]', 0)->plaintext);
		    		$oddsaway=trim($htmlOdds->find('td[id=avges2]', 0)->plaintext);

		    		$oddshomemacau=0;
		    		$ratemacau=0;
		    		$oddsawaymacau=0;
		    		$tmpTr=$htmlOdds->find('tr[id=5]', 0);
		    		if(isset($tmpTr)){
		    			$oddshomemacau=trim($tmpTr->find("td", 2)->plaintext);
			    		$ratemacau=trim($tmpTr->find("td", 3)->ref);
			    		$oddsawaymacau=trim($tmpTr->find("td", 4)->plaintext);
		    		}
		    		
		    		printf("[gameid=%s] [companyid=%s] [companyname=%s] [oddshome=%s] [rate=%s] [oddsaway=%s] [oddshomemacau=%s] [ratemacat=%s] [oddsawaymacau=%s]\n", $value->gameid, $companyid, $companyname, $oddshome, $rate, $oddsaway, $oddshomemacau, $ratemacau, $oddsawaymacau);
			    	$gameInfoObj=GameInfo::model()->find(array('condition'=>'gameid=:gameid','params'=>array(':gameid'=>$value->gameid)));
					if (isset($gameInfoObj)){
						$gameInfoObj->oddshome=$oddshome;
						$gameInfoObj->rate=$rate;
						$gameInfoObj->oddsaway=$oddsaway;
						$gameInfoObj->oddshomemacau=$oddshomemacau;
						$gameInfoObj->ratemacau=$ratemacau;
						$gameInfoObj->oddsawaymacau=$oddsawaymacau;
						$gameInfoObj->update();
						printf("updated\n");
					}
		    	}

		    	$htmlOdds->clear();
				unset($htmlOdds);
    		}
		
		}
    }
}